<template>
    <div class="pay">
        <div class="container">
            <ul class="cartProcess">
                <li>1.购物车</li>
                <li>2.填写订单</li>
                <li>3.成功提交</li>
            </ul>

            <div class="payBox">
                <div class="title">
                    <p class="success">订单提交成功！去付款</p>
                    <p class="money">
                        应付金额：<span>¥{{ goodsTotal }}</span>
                    </p>
                </div>
                <div class="grey">
                    若要使用企业优惠，请联系客服改价，之后可在“我的订单-未付款”中找到并支付。
                </div>
                <div class="orderNum">订单号：{{ orderCode }}</div>
                <div class="title">
                    <p class="success">选择以下支付方式付款</p>
                    <p class="money"></p>
                </div>
                <div class="payList">
                    <div class="wxpay" @click="paying">
                        <img
                            src="https://www.tp-linkshop.com.cn/Content/themes/base/images/Pay/WePayLogo.png"
                        />
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { goPay } from "../../api/testApi/pay";

export default {
    created() {
        let orderInfo = this.$route.query;
        this.cartList = JSON.parse(orderInfo.cartList);
        this.adrId = orderInfo.adrId;
        console.log(this.adrId);
        this.goodsTotal = orderInfo.goodsTotal;
        // 将cartList中的商品id值和num值存入goodsInfo供axios上传订单使用
        Object.keys(this.cartList).forEach((key) => {
            let item = {
                id: this.cartList[key].s_good.id,
                num: this.cartList[key].num,
            };
            this.goodsInfo.push(item);
        });

        // 将cartList中的购物车id值存入cartId供axios上传订单使用
        Object.keys(this.cartList).forEach((key) => {
            let item = this.cartList[key].id;
            this.cartId.push(item);
        });
    },
    data() {
        return {
            cartList: [],
            adrId: 0,
            goodsTotal: 0,
            goodsInfo: [],
            cartId: [],
            // 订单号
            orderCode: 0,
        };
    },
    methods: {
        // 点击支付
        async paying() {
            let data = {
                addr_id: this.adrId,
                project_id: 90,
                goods_info: this.goodsInfo,
                shoppingCartIds: this.cartId,
            };
            let token = sessionStorage.getItem("token");
            let res = await goPay(data, token);
            console.log(res);
            if (res.status == 200) {
                this.orderCode = res.data.result.order_id;
                alert("支付成功,为您跳转到订单页面");
                this.$router.push({ path: "/my" });
            } else {
                alert("支付失败");
            }
        },
    },
};
</script>

<style lang="scss" scoped>
.pay {
    width: 100%;
    min-width: 1200px;
    .container {
        width: 1200px;
        margin: 0 auto;
        .cartProcess {
            width: 100%;
            height: 60px;
            li {
                display: flex;
                justify-content: center;
                align-items: center;
                float: left;
                width: 400px;
                height: 60px;
                border-bottom: 1px solid #e6e6e6;
                color: #333;
                font-size: 18px;
                &:last-child {
                    color: skyblue;
                    border-color: skyblue;
                }
            }
        }
        .payBox {
            .title {
                margin: 48px 0 20px;
                .success {
                    font-weight: 700;
                    width: 990px;
                    display: inline-block;
                    text-align: left;
                }
                .money {
                    font-size: 18px;
                    color: #333;
                    display: inline-block;
                    text-align: right;
                    width: 200px;
                    span {
                        font-size: 18px;
                        color: #ff3232;
                    }
                }
            }
            .grey {
                font-size: 14px;
                color: #666;
                line-height: 20px;
                text-align: left;
            }
            .orderNum {
                color: #333;
                line-height: 20px;
                text-align: left;
                margin: 20px 0 16px;
            }
            .payList {
                margin-bottom: 48px;
                text-align: left;
                .wxpay {
                    display: inline-block;
                    width: 226px;
                    height: 64px;
                    border: 1px solid #ccc;
                    border-radius: 2px;
                    cursor: pointer;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                }
            }
        }
    }
}
</style>
